package com.okq.dao;

import com.okq.dao.base.Impl.BaseDao;
import com.okq.entity.Job;
import com.okq.util.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述 : 工单Dao
 * 作者 : zdl
 * 日期 : 2017/6/29 10:03
 */
@Repository
public class JobDao extends BaseDao {
    public void saveJob(Job job) {
        super.save(job);
    }

    public void deleteJob(Job job) {
        super.delete(job);
    }

    public void updateJob(Job job) {
        super.update(job);
    }

    public void saveOrUpdateJob(Job job) {
        super.saveOrUpdate(job);
    }

    public Job getJobById(String id) {
        return (Job) super.get(Job.class, id);
    }

    public List<Job> listAllJob() {
        String hql = "FROM Job WHERE isDelete = 0";
        return super.find(hql);
    }

    public Page<Job> pageByJob(Page<Job> page, Job job) {
        StringBuilder hqlSb = new StringBuilder();
        hqlSb.append("FROM Job j WHERE j.isDelete = 0 ");
        if (job != null) {
            Map<String, Object> param = new HashMap<>();
            String customerIds = job.getCustomerIds();
            if (StringUtils.isNotBlank(customerIds)) {
                hqlSb.append(" AND j.customerId IN (?1) ");
                param.put("1", customerIds);
            }
            String code = job.getCode();
            if (StringUtils.isNotBlank(code)) {
                hqlSb.append(" AND j.code LIKE ?2 ");
                param.put("2", "%" + code + "%");
            }
            Integer state = job.getState();
            if (state != null && state != -1) {
                hqlSb.append(" AND j.state = ?3 ");
                param.put("3", state);
            }
            String startTime = job.getStartTime();
            if (StringUtils.isNotBlank(startTime)) {
                hqlSb.append(" AND j.createTime >= ?4 ");
                param.put("4", startTime);
            }
            String endTime = job.getEndTime();
            if (StringUtils.isNotBlank(endTime)) {
                hqlSb.append(" AND j.createTime <= ?5 ");
                param.put("5", endTime);
            }
            hqlSb.append(" ORDER BY j.state DESC , j.createTime DESC ");
            return super.findByPage(page, hqlSb.toString(), param);
        }
        return page;
    }

    /**
     * 根据工单编号获取工单
     *
     * @param code 工单编号
     * @return job
     */
    public Job getJobByCode(String code) {
        if (StringUtils.isNotBlank(code)) {
            String hql = "FROM Job WHERE isDelete = 0 AND code = '" + code + "' ";
            return (Job) super.get(hql);
        }
        return null;
    }

    /**
     * 获取最后一条工单
     * @return job
     */
    public Job getLastJob(){
        String hql = "FROM Job WHERE isDelete = 0 ORDER BY createTime DESC ";
        List<Job> jList = super.findMax(hql,1);
        if (jList != null && jList.size() > 0) {
            return jList.get(0);
        } else {
            return null;
        }
    }
}
